package model;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import service.FriendSale;
import tools.Util;

public class History {

	public String text;
	public Date time;
	
	public History(String text, Date time) {
		super();
		this.text = text;
		this.time = time;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public Date getTime() {
		return time;
	}
	public void setTime(Date time) {
		this.time = time;
	}
	
	public String displayText(String viewerid){
		String raw = text;
		Pattern p = Pattern.compile("\\[(\\S+?)\\]");
		Matcher m = p.matcher(raw);

		while(m.find()){
			String matchid = m.group(1);
			String name;
			String id = Util.getId(viewerid);
			String domain = Util.getDomain(viewerid);
			if(matchid.equals(id)){
				name = "我";
			}else{
				SaleData udata = FriendSale.getDataObj(Util.convertToGlobal(domain, matchid));
				if(udata != null){
					name = udata.getPerson().getName();
				}else{
					name = "unknown";
				}
			}
			raw = raw.replace(m.group(), name);
		}
		
		return raw;
	}
	public String displayTime(){
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern("yyyy-MM-dd");// hh:mm:ss");
		return sdf.format(time);
	}
	
	public static void main(String a[]){
//		FriendSale.getData("14859913123498802880");
//		FriendSale.init("14859913123498802880", "liyi", "pic.pic");
//		
//		FriendSale.getData("05230217326271654280");
//		FriendSale.init("05230217326271654280", "simba", "pic.pic");
//		
//		String raw="[14859913123498802880]以¥500的价格买了[05230217326271654280],并起了个绰号叫\"ssss\" ";
//		History h = new History(raw,new Date());
//		
//		System.out.println(h.displayText("14859913123498802880"));
//		System.out.println(h.displayTime());
	}
}
